package com.bookm.bookm.service.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanHandler;
import com.bookm.bookm.entity.Configuration;
import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.Configurationservice;
import com.bookm.bookm.utils.DBUtil;
import com.bookm.bookm.utils.ResultUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConfigurationserviceImpl implements Configurationservice {

    @Override
    //定义一个函数名
    public Result update(String key, String val){
        int rst=0;
        // update into xx(a,b) values(cc,cc)
        try {
            //用SQL语句修改Configuration数据库中的数据
             rst = Db.use().update(Entity.create().set("val",val),Entity.create("configuration").set("skey",key));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //判断是否能正常修改
        if(rst == 1){
            return ResultUtil.success(rst);
        }else {
            return ResultUtil.failure();
        }
    }

    @Override
    public Result getConfiguration() {
        List<Configuration> configurationList = null;
        try {
            configurationList = Db.use().findAll(Entity.create("configuration"),Configuration.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(configurationList == null){
            return ResultUtil.success(configurationList);
        }else {
            return  ResultUtil.failure();
        }
    }

    @Override
    public String getval(String key) {
        Configuration findval = null;
        try {
            findval = Db.use().find(Entity.create("configuration").set("skey", key), BeanHandler.create(Configuration.class));
        }catch (SQLException e){
            e.printStackTrace();
        }
        if(findval == null){
            return "";
        }else {
            return findval.getVal();
        }
    }
}
